#coding=utf-8
import cache
import status

def handler(client,ip_str,data):
    ret = [{"rep":data["req"],"status":status.SUCCESS},[],{},False]
    session = cache.get_session(client)
    if not session:
        ret[0]["status"] = status.FAILED
        return ret
    if session["token"] !=  data["token"]:
        ret[0]["status"] = status.TOKEN_ERR
        return ret
    r_client = cache.get_session_by_name(data["receiver"])
    if r_client and r_client["client"] != client:
        ret[1].append(r_client["client"])
        recv_msg = {"rep":"recv_msg","data":data["msg"]["data"],"sender":session["name"]}
        ret[2] = recv_msg
    return ret
